import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

import Login from "@/views/login/index.vue";
import Layout from "@/layout/index.vue";
import Home from "@/views/home/index.vue";
import JumpEmpty from "@/components/EmptyJump.vue";
import SceneryCondition from "@/views/scenery/query/condition.vue";
import ScenerySpatial from "@/views/scenery/query/spatial.vue";
import SceneryThree from "@/views/scenery/three/index.vue";
import SceneryBlogs from "@/views/scenery/blog/list.vue";
import SceneryEdior from "@/views/scenery/blog/editor.vue";
import SceneryPost from "@/views/scenery/blog/post.vue";
import SceneryJump from "@/views/scenery/blog/jump.vue";
import SurroundHouse from "@/views/surround/accommodation/index.vue";
import SurroundFood from "@/views/surround/dining/index.vue";
import SurroundShop from "@/views/surround/shop/index.vue";
import Personal from "@/views/personal/index.vue";
import Admin from "@/views/admin/index.vue";
import AdminUsers from "@/views/admin/users/index.vue";
import AdminScenery from "@/views/admin/scenery/index.vue";
import AdminHouse from "@/views/admin/accommodation/index.vue";
import AdminFood from "@/views/admin/dining/index.vue";
import AdminShop from "@/views/admin/shop/index.vue";
import MySpace from "@/views/personal/myspace/index.vue";
import changeInfo from "@/views/personal/info/index.vue";
import ShowBlog from "@/views/personal/showBlog/index.vue";
import WriteBlog from "@/views/personal/writeBlog/index.vue";
import EditBlog from "@/views/personal/editBlog/blog.vue";
import EditDraft from "@/views/personal/editBlog/draft.vue";
import BlogCollection from "@/views/personal/collection/blog.vue";
import ScenicCollection from "@/views/personal/collection/scenic.vue";

// 固定路由: 包括登录页面、首页、景区服务、周边服务、用户功能
const routes = [
  {
    path: "/login",
    name: "登录注册页面",
    component: Login,
    meta: {
      title: "用户登录",
    },
  },
  {
    path: "/", // 项目启动时的路由
    name: "layout",
    redirect: window.localStorage.getItem("token") == null ? "/login" : "/home",
    component: Layout,
    children: [
      {
        path: "/home",
        name: "首页", // 设置路由的名称, 不能重复
        component: Home,
        meta: {
          title: "iTravel智慧旅游服务平台", // 设置网站标签页的标题
        },
      },
      {
        path: "/goto",
        name: "跳转空白页", // 设置路由的名称, 不能重复
        component: JumpEmpty,
      },
      {
        path: "/scenery/condition",
        name: "景区服务-条件查询",
        component: SceneryCondition,
        meta: {
          title: "景区条件查询",
        },
      },
      {
        path: "/scenery/spatial",
        name: "景区服务-空间查询",
        component: ScenerySpatial,
        meta: {
          title: "景区空间查询",
        },
      },
      {
        path: "/scenery/three",
        name: "景区服务-三维展示",
        component: SceneryThree,
        meta: {
          title: "景区三维展示",
        },
      },
      {
        path: "/scenery/blogs",
        name: "景区服务-游记广场",
        component: SceneryBlogs,
        meta: {
          title: "游记广场",
        },
      },
      {
        path: "/scenery/editor",
        name: "景区服务-游记创作",
        component: SceneryEdior,
        meta: {
          title: "游记创作",
        },
      },
      {
        path: "/scenery/jump/:type",
        name: "景区服务-页面跳转",
        component: SceneryJump,
        meta: {
          title: "游记创作",
        },
      },
      {
        path: "/scenery/post/:article_id",
        name: "景区服务-游记详情",
        component: SceneryPost,
        meta: {
          title: "游记详情",
        },
      },
      {
        path: "/surround/accommodation",
        name: "周边服务-旅游住宿",
        component: SurroundHouse,
        meta: {
          title: "旅游住宿服务",
        },
      },
      {
        path: "/surround/dining",
        name: "周边服务-旅游餐饮",
        component: SurroundFood,
        meta: {
          title: "旅游餐饮服务",
        },
      },
      {
        path: "/surround/shop",
        name: "周边服务-旅游购物",
        component: SurroundShop,
        meta: {
          title: "旅游购物服务",
        },
      },
      {
        path: "/personal",
        name: "个人中心",
        component: Personal,
        redirect: "/personal/mySpace",
        meta: {
          title: "个人中心",
        },
        children: [
          {
            path: "/personal/mySpace",
            name: "个人中心-主页",
            component: MySpace,
            meta: {
              title: "个人中心-主页",
            },
          },
          {
            path: "/personal/changeInfo",
            name: "个人中心-账号管理",
            component: changeInfo,
            meta: {
              title: "个人中心-账号管理",
            },
          },
          {
            path: "/personal/showBlog",
            name: "个人中心-文章展示",
            component: ShowBlog,
            meta: {
              title: "个人中心-文章",
            },
          },
          {
            path: "/personal/post/:article_id",
            name: "个人中心-文章详情",
            component: SceneryPost,
            meta: {
              title: "个人中心-文章详情",
            },
          },
          {
            path: "/personal/writeBlog",
            name: "个人中心-文章书写",
            component: WriteBlog,
            meta: {
              title: "个人中心-文章",
            },
          },
          {
            path: "/personal/editBlog/draft/:article_id",
            name: "个人中心-修改草稿",
            component: EditDraft,
            meta: {
              title: "个人中心-草稿编辑",
            },
          },
          {
            path: "/personal/editBlog/publish/:article_id",
            name: "个人中心-修改游记",
            component: EditBlog,
            meta: {
              title: "个人中心-游记编辑",
            },
          },
          {
            path: "/personal/collection/blogCollection",
            name: "个人中心-文章收藏",
            component: BlogCollection,
            meta: {
              title: "个人中心-收藏",
            },
          },
          {
            path: "/personal/collection/scenicCollection",
            name: "个人中心-景点收藏",
            component: ScenicCollection,
            meta: {
              title: "个人中心-收藏",
            },
          },
        ],
      },
    ],
  },
];

// 系统管理部分对应的路由, 若是管理员用户登录本系统, 则将该路由添加到项目路由
const adminRoutes = {
  path: "/admin",
  name: "系统管理",
  redirect: "/admin/users",
  component: Admin,
  children: [
    {
      path: "/admin/users",
      name: "系统管理-用户管理",
      component: AdminUsers,
      meta: {
        title: "用户管理",
      },
    },
    {
      path: "/admin/scenery",
      name: "系统管理-景区管理",
      component: AdminScenery,
      meta: {
        title: "景区管理",
      },
    },
    {
      path: "/admin/accommodation",
      name: "系统管理-旅游住宿管理",
      component: AdminHouse,
      meta: {
        title: "住宿场所管理",
      },
    },
    {
      path: "/admin/dining",
      name: "系统管理-旅游餐饮管理",
      component: AdminFood,
      meta: {
        title: "餐饮场所管理",
      },
    },
    {
      path: "/admin/shop",
      name: "系统管理-购物场所管理",
      component: AdminShop,
      meta: {
        title: "购物场所管理",
      },
    },
  ],
};

// 方法功能：管理员用户登录时，添加系统管理部分对应的路由
// 调用时机：需要在用户登录和页面手动刷新之后调用

export async function addAdminRoutes() {
  // 重置路由
  router.matcher = createRouter().matcher;
  // 添加系统管理模块的路由
  router.addRoute("layout", adminRoutes);
  router.options.isAddAsyncRoute = true;
}

const createRouter = () =>
  new VueRouter({
    mode: "history",
    scrollBehavior: () => ({ y: 0 }),
    routes: routes,
  });

const router = createRouter();

export default router;
